A button is a control, which is an interactive component that enables users to communicate with an application. The Button class inherits directly from the ButtonBase class. A Button can be clicked by using the mouse, ENTER key, or SPACEBAR if the button has focus.
Drag and drop Button and TextBox control from toolbox on windows form.
Drag and drop a TextBox and label as Enter Name .
Write code on Button click
private void btnSubmit_Click(object sender, EventArgs e)
{
//MessageBox will show the text entered in textbox
MessageBox.Show("Entered name is "+textBox1.Text);
}
Run The Project:
When you insert any text and click submit button then inserted text will show in the Message box.
Set a Button as the Cancel Button.
On Windows Form you can designate a Button control to be the cancel button. A cancel button is clicked whenever the user presses the ESC key.
private void btnSubmit_Click(object sender, EventArgs e)
{
this.Close(); //close window Form
}
private void Form2_Load(object sender, EventArgs e)
{
// Set button as cancel button
this.CancelButton = btnSubmit;
}
When you press Esc key on the keyboard then Window Form will closed.
Properties of Button:
BackColor
Through BackColor properties we can change BackColor of Button.
private void Form2_Load(object sender, EventArgs e)
{ //change button backcolor
btnSubmit.BackColor = Color.CadetBlue;
}
Output:
At run time backcolor of button will be change.
ForeColor:
private void Form2_Load(object sender, EventArgs e)
{
//change forecolor of button text
btnSubmit.ForeColor = Color.Red;
}
At run time ForeColor of button will be change.
Output:
Leave Comment
2 Comments
View All Comments